<template>
	<a-card :bordered="false" :body-style="{ 'padding-bottom': '0px' }" class="mb-2">
		<a-form ref="formRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="5">
					<a-form-item label="订单号" name="orderNo">
						<a-input v-model:value="searchFormState.orderNo" allow-clear></a-input>
					</a-form-item>
				</a-col>
				<a-col :span="5">
					<a-form-item label="卡号" name="cardNo">
						<a-input v-model:value="searchFormState.cardNo" allow-clear></a-input>
					</a-form-item>
				</a-col>
				<a-col :span="8">
					<a-form-item label="创建时间：" >
						<div style="display: flex;align-items: center;">
							<a-form-item style="flex: 1;margin-right:12px;" name="startTimeCreated">
								<a-date-picker placeholder="请选择开始日期" class="block" format="YYYY-MM-DD" v-model:value="searchFormState.startTimeCreated" allow-clear/>
							</a-form-item>
							<a-form-item style="flex: 1" name="endTimeCreated">
								<a-date-picker placeholder="请选择结束日期" class="block" format="YYYY-MM-DD" v-model:value="searchFormState.endTimeCreated" allow-clear/>
							</a-form-item>
						</div>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" html-type="submit" @click="table.refresh()">查询</a-button>
					<a-button style="margin: 0 8px" @click="() => formRef.resetFields()">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
	</a-card>
	<a-card :bordered="false">
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" :loading="exporting" @click="exportFile()">导出</a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'rechargeInfo'">
					<div>储值本金：{{ record.rechargeAmount }}</div>
					<div>赠送本金：{{ record.presentValue }}</div>
					<div>当前余额：{{ record.currentAmount }}</div>
				</template>
				<template v-if="column.dataIndex === 'cardInfo'">
					<div>卡号：{{ record.cardNo }}</div>
					<a-tag v-if="record.cardType === 'CARD'" color="blue">
						储值卡
					</a-tag>
					<a-tag v-else color="green">
						会员卡
					</a-tag>
				</template>
				<template v-if="column.dataIndex === 'channel'">
					<div v-if="record.channel === 'APP'">小程序</div>
					<div v-else-if="record.channel === 'POS'">收银系统</div>
					<div v-else-if="record.channel === 'OFFLINE'">后台系统</div>
					<div v-else-if="record.channel === 'DINGTALK'">钉钉</div>
				</template>
				<template v-if="column.dataIndex === 'status'">
					{{ $TOOL.dictTypeData('VOUCHER_ORDER_STATUS', record.status) }}
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-button :disabled="record.status !== 'ERROR'" :loading="redoLoading" type="link" size="small"  @click="handleRedo(record)">重新充值</a-button>
				</template>
			</template>
		</s-table>
	</a-card>
</template>

<script setup name="crmVoucher">
	import { message } from 'ant-design-vue'
	import crmApi from '@/api/crm/crmApi'
	import tool from '@/utils/tool'
	let searchFormState = reactive({})
	const formRef = ref()
	const table = ref(null)
	let form = ref()
	const exporting = ref(false)
	const redoLoading = ref(false)
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: true }
	const columns = [
		{
			title: '卡信息',
			dataIndex: 'cardInfo',
			width: 240
		},
		{
			title: '订单号',
			dataIndex: 'orderNo',
			width: 220
		},
		{
			title: '充值渠道',
			dataIndex: 'channel',
			width: 120,
			align: 'center'
		},
		{
			title: '储值结果',
			dataIndex: 'rechargeInfo',
			width: 160
		},
		{
			title: '订单状态',
			dataIndex: 'status',
			width: 120,
			align: 'center'
		},
		{
			title: '创建时间',
			dataIndex: 'createdTime',
			ellipsis: true,
			sorter: true,
			width: 200,
			align: 'center'
		},
		{
			title: '操作',
			dataIndex: 'action',
			align: 'center'
		}
	]
	let selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		alert: {
			show: false,
			clear: () => {
				selectedRowKeys = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const exportFile = () => {
		exporting.value = true
		const params = Object.assign({}, searchFormState)
		if (params.startTimeCreated) {
			params.startTimeCreated = params.startTimeCreated.format('YYYY-MM-DD')
		}
		if (params.endTimeCreated) {
			params.endTimeCreated = params.endTimeCreated.add(1,'day').format('YYYY-MM-DD')
		}
		params.pageNo = null
		crmApi.exportExcel('voucherOrder/exportList', '储值查询' + Date.now(), params).finally(_ => {
			exporting.value = false
		})
	}
	// 列表数据
	const loadData = (parameter) => {
		const params = Object.assign(parameter, searchFormState)
		if (params.startTimeCreated) {
			params.startTimeCreated = params.startTimeCreated.format('YYYY-MM-DD')
		}
		if (params.endTimeCreated) {
			params.endTimeCreated = params.endTimeCreated.add(1,'day').format('YYYY-MM-DD')
		}
		return crmApi.voucherOrderPage(params).then((res) => {
			return res
		})
	}
	// 重新充值
	const handleRedo = (record) => {
		redoLoading.value = true
		return crmApi.voucherOrderRedo([record]).then(() => {
			table.value.refresh(true)
		}).finally(_ => redoLoading.value = false)
	}
	// 删除
	const remove = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		crmApi.voucherOrderRemove(params).then(() => {
			table.value.refresh(true)
		})
	}
	// 批量删除
	const removeBatch = () => {
		if (selectedRowKeys.value.length < 1) {
			message.warning('请选择一条或多条数据')
			return false
		}
		const params = selectedRowKeys.value.map((m) => {
			return {
				id: m
			}
		})
		crmApi.voucherOrderRemove(params).then(() => {
			table.value.clearRefreshSelected()
		})
	}


</script>

<style scoped>

</style>
